package com.cynteka.cyfoman.core.collection;

import com.cynteka.cyfoman.core.ComplexPropertyResolver;
import com.cynteka.cyfoman.core.IPropertyResolver;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Pattern;

/**
 * User: Sergey
 * Date: 01.08.11
 * Time: 20:16
 */
public class FileLinkAppender implements IRegistryAppender {

    private String linkId = "propertyToFileLink";
    private String collectionSourceId;
    private String collectionDestinationId;
    private LinkType linkType = LinkType.MANY_TO_MANY;
    private String pathSeparator = File.pathSeparator;
    private String fileNameProperty = "file";
    private IPropertyResolver propertyResolver = new ComplexPropertyResolver();

    public class Link extends BaseLink<Object, File> {

        public Link() {
            super(linkId, collectionSourceId, collectionDestinationId, linkType);
        }

        @Override
        public Collection<File> getDestinationItems(Object o) {
            Object oFiles = propertyResolver.getProperty(o, fileNameProperty);
            String[] fileNames;
            if (oFiles != null) {
                String sFile = String.valueOf(oFiles);
                fileNames = sFile.split(Pattern.quote(pathSeparator));
            } else {
                fileNames = new String[0];
            }
            Collection<File> files = new ArrayList<File>();

            IFileCollection destCollection = (IFileCollection) getOwner().getCollection(getCollectionDestinationId());
            for (String fileName : fileNames) {
                File file = new File(destCollection.getBaseFolder(), fileName);
                files.add(file);
            }

            return files;
        }

        @Override
        public Collection<Object> getSourceItems(File file) {
            // TODO investigate if it's possible to implement it
            return null;
        }
    }


    @Override
    public ICollection[] getCollections() {
        return new ICollection[0];
    }

    @Override
    public ILink[] getLinks() {
        return new ILink[]{new Link()};  //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getLinkId() {
        return linkId;
    }

    public void setLinkId(String linkId) {
        this.linkId = linkId;
    }

    public String getCollectionSourceId() {
        return collectionSourceId;
    }

    public void setCollectionSourceId(String collectionSourceId) {
        this.collectionSourceId = collectionSourceId;
    }

    public String getCollectionDestinationId() {
        return collectionDestinationId;
    }

    public void setCollectionDestinationId(String collectionDestinationId) {
        this.collectionDestinationId = collectionDestinationId;
    }

    public LinkType getLinkType() {
        return linkType;
    }

    public void setLinkType(LinkType linkType) {
        this.linkType = linkType;
    }

    public String getPathSeparator() {
        return pathSeparator;
    }

    public void setPathSeparator(String pathSeparator) {
        this.pathSeparator = pathSeparator;
    }

    public String getFileNameProperty() {
        return fileNameProperty;
    }

    public void setFileNameProperty(String fileNameProperty) {
        this.fileNameProperty = fileNameProperty;
    }

    public IPropertyResolver getPropertyResolver() {
        return propertyResolver;
    }

    public void setPropertyResolver(IPropertyResolver propertyResolver) {
        this.propertyResolver = propertyResolver;
    }
}
